import wget
import os
import argparse


def create_parser():
    parser = argparse.ArgumentParser(description="py build mindspore", add_help=False)
    parser.add_argument("--mindspore_dir", type=str, default='mindspore', help="mindspore dir")
    parser.add_argument("--j", type=int, default=8, help="compiler thread nums")
    parser.add_argument("--down_dir", type=str, default='build_package', help="build mindspore package download dir")
    args, _ = parser.parse_known_args()
    return args


def get_build_package(mindspore_dir):
    os.environ["LC_ALL"] = "en_US.UTF-8"
    os.environ["LANG"] = "en_US.UTF-8"

    grep_dict = {'github_tar': '"https://github.com.*.tar.*"',
                 'github_zip': '"https://github.com.*.zip"',
                 'gitlab_tar': '"https://gitlab.com.*.tar.*"',
                  'gitlab_zp': '"https://gitlab.com.*.zip"'
                 }

    file_list = {}
    for key in grep_dict.keys():
        grep_cmd = 'cd {} && grep {} {} -anr > {}'.format(mindspore_dir, grep_dict.get(key), 'cmake', key + '.txt')
        print('grep_cmd: ', grep_cmd, ' ', os.path.abspath('{}.txt'.format(key)))
        os.system(grep_cmd)
        file_list[key] = os.path.abspath(os.path.join(mindspore_dir, key + '.txt'))
    return file_list


def read_txt(file_path):
    with open(file_path, 'r', encoding='utf-8') as f:
        files = f.readline()
    return files


def get_download_url(file_list):
    for key in file_list:
        file_path = file_list.get(key)
        files = read_txt(file_path)
        print(files)
        break


if __name__ == '__main__':
    args = create_parser()
    file_list = get_build_package(args.mindspore_dir)
    print(file_list)
